#include <stdio.h>
#include <assert.h>

#include <CompileVisitor.h>
#include <Assembler.h>

#include "AstNodeCompareExpression.h"

AstNodeCompareExpression::AstNodeCompareExpression(
    AstNodeExpression * left, AstNodeExpression  * rite, unsigned char cmpOperator)
{
  this->left=left;
  this->rite=rite;
  this->oper=cmpOperator;
}


void AstNodeCompareExpression::dump(int level)
{
  printf("cmp ");
  left->dump(level+1);
  switch(oper) {
    case _equal:
      printf("==");
      break;
    case _less:
      printf("<");
      break;
    case _greater:
      printf(">");
      break;
    case _less_equal:
      printf("<=");
      break;
    case _greater_equal:
      printf(">=");
      break;
    case _not_equal:
      printf("<>");
      break;
    default:
      assert(0);
  }
  printf(" ");
  rite->dump(level+1);
}

unsigned char AstNodeCompareExpression::getOpcodeForFalse()
{
  switch(oper) {
    case _equal:
      return OPC_ifne;
    case _less:
      return OPC_ifge;
    case _greater:
      return OPC_ifle;
    case _less_equal:
      return OPC_ifgt;
    case _greater_equal:
      return OPC_iflt;
    case _not_equal:
      return OPC_ifeq;
    default:
      assert(0);
  }
}

void AstNodeCompareExpression::compile(CompileVisitor * visitor)
{
  visitor->compile(this);
}

#if 0
int AstNodeCompareExpression::compile(ClassFile * klass, MethodInfo * method)
{
  switch(oper) {
    case _equal:
      left->compile(klass,method);
      rite->compile(klass,method);
      method->assemble(OPC_dcmpg);
      break;
    case _less:
      left->compile(klass,method);
      rite->compile(klass,method);
      method->assemble(OPC_dcmpg);
      break;
    case _greater:
      left->compile(klass,method);
      rite->compile(klass,method);
      method->assemble(OPC_dcmpg);
      break;
    case _less_equal:
      left->compile(klass,method);
      rite->compile(klass,method);
      method->assemble(OPC_dcmpg);
      break;
    case _greater_equal:
      left->compile(klass,method);
      rite->compile(klass,method);
      method->assemble(OPC_dcmpg);
      break;
    case _not_equal:
      left->compile(klass,method);
      rite->compile(klass,method);
      method->assemble(OPC_dcmpg);
      break;
    default:
      assert(0);
  }
  return 0;
}
#endif
